<template>
	<view class="components">
		<view class="list">
			<!-- 列表 -->
			<view v-show="type != '2'" v-for="(item, index) in list" :key="index" class="item" @click="
			type != '2'? to('/pages/CommunityPages/details/main?id=' + item.id): ''">
				<view class="userInfo">
					<view class="headImg">
						<image :src="item.avatar || '/static/images/default_avatar.png'" mode="aspectFill" />
						<view>{{ item.nickName || '' }}</view>
					</view>
					<div class="solution" v-if="item.helpStatus == 1">已解决</div>
				</view>
				<div v-if="item.recourseAmount > 0" class="reward">悬赏{{item.recourseAmount}}积分</div>
				<view class="title">
					<text class="good" v-if="item.publishStatus == 2">精</text>
					<text class="stick" v-if="item.publishStatus == 1">顶</text>
					<text class="ask" v-if="item.publishType == 1">问</text>
					<text v-if="item.publishSubject" class="text">{{ item.publishSubject }}</text>
					<text v-if="item.publishContent" class="text">{{ item.publishContent }}</text>
				</view>
				<div v-if="item.showImage" class="img-box" @click.stop>
					<img class="img" v-for="(x, y) in item.imgs" :key="y" :src="x" @click="preview(x, item.imgs)">
				</div>
				<view class="btn-primary" v-if="item.imgs.length > 0" @click.stop="changeImageStatus(index)">
					{{item.showImage ? '隐藏图片' : '查看图片'}}
				</view>
				<view class="btn-primary" v-if="item.videoUrl" @click.stop="playVideo(item)">播放视频</view>
				<view class="foot">
					<view class="date">{{ item.createTime }}</view>
					<view class="detail">
						<view v-if="type == 1">
							<image src="../static/images/ic_see.png" />
							<view>{{ item.readingAmount }}</view>
						</view>
						<view v-if="type == 1">
							<image src="../static/images/ic_comment.png" />
							<view>{{ item.commentsAmount }}</view>
						</view>
						<view v-if="type == 2">
							<image src="../static/images/ic_praise.png" />
							<view>{{ item.likesAmount }}</view>
						</view>
					</view>
				</view>
			</view>
			<!-- 用户详情 -->
			<view v-if="type == '2'" class="item">
				<view class="userInfo">
					<view class="headImg" @click="to('/pages/MinePages/friend/main?id=' + details.userId)">
						<image :src="details.avatar || '/static/images/default_avatar.png'" mode="aspectFill"></image>
						<view>{{ details.nickName || ''}}</view>
					</view>
					<view @click="add( details.isAttention ?  1 : 0 )" :class="details.isAttention ? 'add gray' : 'add'">
						{{details.isAttention ?'已关注':'关注'}}
					</view>
				</view>
				<view class="title">
					<text v-if="details.publishSubject" class="text">{{ details.publishSubject || '' }}</text>
					<text class="text">{{ details.publishContent || '' }}</text>
				</view>
				<div v-if="details.showImage" class="img-box" @click.stop>
					<img class="img" v-for="(x, y) in details.imgs" :key="y" :src="x" @click="preview(x, details.imgs)">
				</div>
				<view class="btn-primary" v-if="details.imgs.length > 0" @click.stop="details.showImage = !details.showImage">
					{{details.showImage ? '隐藏图片' : '查看图片'}}
				</view>
				<view class="btn-primary" v-if="details.videoUrl" @click.stop="playVideo(details)">播放视频</view>
				<view class="foot">
					<view class="date">{{ details.createTime }}</view>
					<view class="detail">
						<view @click="likes">
							<image mode="aspectFill" v-if="details.status == '0'" src="/static/images/ic_praise.png"
								style="width: 30rpx;height: 30rpx;" />
							<image style="width: 30rpx;height: 30rpx;" mode="aspectFill" v-else
								src="/static/images/ic_praise_active.png" />
							<view style="margin-left: 10rpx;">{{ details.likesAmount }}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getpostList,
		getPublishInfoDetails,
		addMyFriend,
		getListByUser,
		getMyCollectionList,
		addFocus,
		cancelFocus
	} from "@/apis/request";

	export default {
		props: {
			type: {
				//type为1代表列表页组件，2为详情页组件,3为用户帖子,4我的收藏
				default: 1
			},
			status: {
				default: 0
			},
			groupId: {
				default: ""
			},
			publishId: {
				default: ""
			},
			info: {
				collectStatus: "",
				friendStatus: ""
			},
		},
		data() {
			return {
				pageNo: 1,
				list: [],
				finished: false,
				details: {
					showImage: false,
					imgs: []
				}
			};
		},
		watch: {
			publishId: {
				immediate: true,
				handler(newName, oldName) {
					if (newName) {
						this.getdetails();
					}
				}
			},
			groupId: {
				immediate: true,
				handler(newName, oldName) {
					if (newName) {
						this.refreshList();
					}
				}
			},
			type: {
				immediate: true,
				handler(newName, oldName) {
					if (newName) {
						this.refreshList();
					}
				}
			},
			status: {
				immediate: true,
				handler(newvale, oldvale) {
					if (newvale) {
						this.refreshList();
					}
				}
			}
		},
		methods: {
			async getMyCollectionList() {
				if (!this.finished) {
					const data = await getMyCollectionList({
						pageNo: this.pageNo++,
						pageSize: 10
					});
					if (data.records.length == 0) {
						this.finished = true;
					}
					this.list.push(
						...data.records.map(item => {
							return {
								...item,
								imgs: item.imageUrl ? item.imageUrl.split(",") : []
							};
						})
					);
					uni.stopPullDownRefresh();
				}
			},
			async getUserlist() {
				if (!this.finished) {
					const data = await getListByUser({
						pageNo: this.pageNo++,
						pageSize: 10
					});
					if (data.records.length == 0) {
						this.finished = true;
					}
					this.list.push(
						...data.records.map(item => {
							return {
								...item,
								imgs: item.imageUrl ? item.imageUrl.split(",") : []
							};
						})
					);
					uni.stopPullDownRefresh();
				}
			},
			preview(current, imgList) {
				this.$Imgpreview(current, imgList);
			},
			changeImageStatus(index) {
				this.list[index].showImage = !this.list[index].showImage
			},
			add(type) {
				const {
					id: userId
				} = uni.getStorageSync('userInfo')
				if (type) {
					cancelFocus({
						attentionId: this.details.userId,
						attentionType: 2,
						userId
					})
				} else {
					addFocus({
						attentionId: this.details.userId,
						attentionType: 2,
						userId
					});
				}
				setTimeout(() => {
					this.getdetails()
				}, 100)
			},
			likes() {
				if (this.details.status) {
					this.$emit('likesClick', 2)
				} else {
					this.$emit('likesClick', 1)
				}
			},
			async getdetails() {
				this.details = await getPublishInfoDetails({
					publishId: this.publishId
				});
				this.$set(this.details, 'showImage', false);
				this.details.imgs = this.details.imageUrl ?
					this.details.imageUrl.split(",") : [];
				this.$emit("info", {
					collectStatus: this.details.collectStatus, //收藏状态
					friendStatus: this.details.friendStatus //点赞状态
				});
				this.$emit("details", this.details);
			},
			to(url) {
				uni.navigateTo({
					url
				});
			},
			refreshList() {
				this.pageNo = 1;
				this.list = [];
				if (this.type == 4) {
					this.getMyCollectionList();
				} else if (this.type == 3) {
					this.getUserlist();
				} else {
					this.getList();
				}
			},
			async getList() {
				if (!this.finished && this.groupId) {
					const data = await getpostList({
						groupId: this.groupId,
						pageNo: this.pageNo,
						pageSize: 10,
						status: this.status,
						isAllOrMy: 1
					});
					if (data.records.length == 0) {
						this.finished = true;
					}
					if (this.pageNo == 1) {
						this.list = [];
					}
					this.list = this.list.concat(data.records.map(item => {
						return {
							...item,
							showImage: false,
							imgs: item.imageUrl ? item.imageUrl.split(",") : []
						};
					}))
					if (this.list.length < data.total) {
						this.pageNo++;
					}
					uni.stopPullDownRefresh();
				}
			},
			playVideo(item) {
				uni.navigateTo({
					url: '/pages/CommunityPages/video/video?src=' + encodeURIComponent(item.videoUrl)
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.components {
		.list {
			.item {
				padding: 30rpx 20rpx 30rpx 30rpx;
				border-bottom: 1rpx solid #e8e8e8;
				background-color: #ffffff;

				.userInfo {
					position: relative;
					display: flex;
					align-items: center;
					flex-direction: row;

					.headImg {
						display: flex;
						align-items: center;

						image {
							width: 70rpx;
							height: 70rpx;
							border-radius: 50%;
						}

						view {
							margin-left: 10rpx;
							color: #020202;
							font-size: 30rpx;
						}
					}

					.add {
						width: 112rpx;
						height: 40rpx;
						margin-left: 20rpx;
						background: #ffffff;
						border: 1rpx solid #32C59A;
						border-radius: 25rpx;
						font-size: 22rpx;
						color: #32C59A;
						line-height: 40rpx;
						text-align: center;
					}

					.gray {
						color: #999999;
						border: 1rpx solid #DCDCDC;
					}

					.solution {
						position: absolute;
						right: 0;
						color: #FF290B;
						font-size: 26rpx;
						font-weight: bold;
						transform: rotate(45deg);
					}
				}

				.reward {
					margin: 5rpx 0;
					font-size: 28rpx;
					color: #E5AE54;
				}

				.title {
					margin: 10rpx 0rpx 10rpx;

					text {
						font-size: 28rpx;
					}

					.text {
						font-size: 30rpx;
					}

					.ask {
						margin-right: 5rpx;
						padding: 0 5rpx;
						color: #F83F06;
						font-size: 24rpx;
						border: 1rpx solid #F83F06;
						background-color: #FFF3F4;
						border-radius: 5rpx
					}

					.stick {
						margin-right: 5rpx;
						padding: 0 5rpx;
						color: #1DDA61;
						font-size: 24rpx;
						border: 1rpx solid #1DDA61;
						background-color: #E6F9ED;
						border-radius: 5rpx
					}

					.good {
						margin-right: 5rpx;
						padding: 0 5rpx;
						color: #07A0F8;
						font-size: 24rpx;
						border: 1rpx solid #07A0F8;
						background-color: #E7F4FB;
						border-radius: 5rpx
					}
				}

				.imgList {
					display: flex;
					flex-direction: row;

					image {
						width: 222rpx;
						height: 222rpx;
						border-radius: 10rpx;
						margin: 10rpx 10rpx 10rpx 0rpx;
					}
				}

				.img-box {
					display: flex;
					flex-wrap: wrap;

					.img {
						margin: 10rpx 10rpx 0 0;
						width: 222rpx;
						height: 222rpx;
						border-radius: 10rpx;

						&:nth-child(3n) {
							margin-right: 0;
						}
					}
				}

				.btn-primary {
					height: 60rpx;
					width: 200rpx;
					margin: 0 auto;
					margin-top: 20rpx;
					line-height: 60rpx;
					text-align: center;
					background: #32C59A;
					color: #ffffff;
					font-size: 28rpx;
					border-radius: 100rpx;
				}

				.foot {
					margin-top: 20rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.date {
						color: #999999;
						font-size: 22rpx;
					}

					.detail {
						display: flex;
						align-items: center;

						view {
							display: flex;
							align-items: center;
							font-size: 26rpx;
						}

						view:nth-child(1) {
							margin-right: 30rpx;

							image {
								margin-right: 5rpx;
								width: 40rpx;
								height: 26.5rpx;
							}
						}

						view:nth-child(2) {
							image {
								margin-right: 5rpx;
								width: 35rpx;
								height: 35rpx;
							}
						}

						view:nth-child(3) {
							image {
								width: 35rpx;
								height: 35rpx;
							}
						}
					}
				}
			}
		}
	}
</style>